package com.pearadmin.pro.common.tools.core;

import com.google.common.collect.Lists;
import org.dozer.DozerBeanMapper;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author Chenjian
 * @date 2020-06-23 16:40
 */
public class GeneralConvertUtils {
    private static final DozerBeanMapper dozerMapper = new DozerBeanMapper();

    /**
     * 对象通用转换
     *
     * @param source           源对象
     * @param destinationClass 目标类
     * @param <T>
     * @return 返回得到destinationClass
     */
    public static <T> T conv(Object source, Class<T> destinationClass) {
        if (null == source) {
            return null;
        }
        T convObj = dozerMapper.map(source, destinationClass);
        return convObj;
    }

    /**
     * 集合转换
     *
     * @param sourceList       源集合
     * @param destinationClass 目标类
     * @param <T>
     * @return 返回得到destinationClass的集合结果
     */
    public static <T> List<T> convert2List(List<?> sourceList, Class<T> destinationClass) {
        List<T> destinationList = Lists.newArrayList();
        if(ObjectUtils.isEmpty(sourceList)){
            return null;
        }
        sourceList.forEach(source -> {
            destinationList.add(GeneralConvertUtils.conv(source, destinationClass));
        });
        return destinationList;
    }


}
